package com.music.note_android.data.database

import com.music.note_android.data.database.dao.NoteDao
import com.music.note_android.data.database.dao.FolderDao
import com.music.note_android.data.model.Note
import com.music.note_android.data.model.Folder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.util.UUID
import javax.inject.Inject
import javax.inject.Singleton

/**
 * 数据库初始化器
 * 负责在首次启动时创建示例数据
 */
@Singleton
class DatabaseInitializer @Inject constructor(
    private val noteDao: NoteDao,
    private val folderDao: FolderDao
) {
    
    /**
     * 初始化示例数据
     */
    suspend fun initializeSampleData() = withContext(Dispatchers.IO) {
        // 检查是否已有数据
        val noteCount = noteDao.getNotesCount()
        if (noteCount > 0) {
            return@withContext // 已有数据，不需要初始化
        }
        
        val currentTime = System.currentTimeMillis()
        
        // 创建示例文件夹
        val workFolder = Folder(
            id = UUID.randomUUID().toString(),
            name = "工作笔记",
            createdTime = currentTime - 86400000, // 1天前
            isSystem = false
        )
        
        val studyFolder = Folder(
            id = UUID.randomUUID().toString(),
            name = "学习笔记",
            createdTime = currentTime - 172800000, // 2天前
            isSystem = false
        )
        
        folderDao.insertFolder(workFolder)
        folderDao.insertFolder(studyFolder)
        
        // 创建示例笔记
        val sampleNotes = listOf(
            Note(
                id = UUID.randomUUID().toString(),
                title = "欢迎使用笔记应用",
                createdTime = currentTime - 3600000, // 1小时前
                modifiedTime = currentTime - 3600000,
                folderId = null,
                thumbnailPath = null,
                dataPath = "notes/welcome.json"
            ),
            Note(
                id = UUID.randomUUID().toString(),
                title = "今日工作计划",
                createdTime = currentTime - 7200000, // 2小时前
                modifiedTime = currentTime - 7200000,
                folderId = workFolder.id,
                thumbnailPath = null,
                dataPath = "notes/work_plan.json"
            ),
            Note(
                id = UUID.randomUUID().toString(),
                title = "会议记录 - 项目讨论",
                createdTime = currentTime - 10800000, // 3小时前
                modifiedTime = currentTime - 10800000,
                folderId = workFolder.id,
                thumbnailPath = null,
                dataPath = "notes/meeting_notes.json"
            ),
            Note(
                id = UUID.randomUUID().toString(),
                title = "Android开发学习笔记",
                createdTime = currentTime - 14400000, // 4小时前
                modifiedTime = currentTime - 14400000,
                folderId = studyFolder.id,
                thumbnailPath = null,
                dataPath = "notes/android_study.json"
            ),
            Note(
                id = UUID.randomUUID().toString(),
                title = "Kotlin语法要点",
                createdTime = currentTime - 18000000, // 5小时前
                modifiedTime = currentTime - 18000000,
                folderId = studyFolder.id,
                thumbnailPath = null,
                dataPath = "notes/kotlin_syntax.json"
            ),
            Note(
                id = UUID.randomUUID().toString(),
                title = "购物清单",
                createdTime = currentTime - 21600000, // 6小时前
                modifiedTime = currentTime - 21600000,
                folderId = null,
                thumbnailPath = null,
                dataPath = "notes/shopping_list.json"
            )
        )
        
        // 插入示例笔记
        noteDao.insertNotes(sampleNotes)
    }
}